<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二叉树的序列化与反序列化</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #282c34;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #21252b;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            color: #abb2bf;
            line-height: 1.6;
        }
        .keyword { color: #c678dd; }
        .string { color: #98c379; }
        .function { color: #61afef; }
        .number { color: #d19a66; }
        .comment { color: #5c6370; }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <div class="floating-icon inline-block mb-6">
                <i class="fas fa-tree text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6 tracking-tight">
                二叉树的序列化与反序列化
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                探索数据结构与字符串之间的优雅转换，掌握树形结构持久化的核心技术
            </p>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <i class="fas fa-clipboard-list text-3xl text-purple-600 mr-4"></i>
                <h2 class="text-3xl font-bold text-gray-800">题目描述</h2>
            </div>
            <p class="text-lg text-gray-700 leading-relaxed">
                设计一个算法，来序列化和反序列化二叉树。序列化是将数据结构转化为字符串的过程，反序列化是将字符串转化为原始数据结构的过程。你的算法不限制使用何种遍历方法，但要确保一个二叉树可以被序列化为一个字符串并且这个字符串可以被反序列化为原始的树结构。
            </p>
        </div>

        <!-- Core Concepts -->
        <div class="grid md:grid-cols-2 gap-8 mb-8">
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center mb-4">
                    <i class="fas fa-brain text-3xl text-indigo-600 mr-4"></i>
                    <h3 class="text-2xl font-bold text-gray-800">核心考点</h3>
                </div>
                <div class="flex flex-wrap gap-3">
                    <span class="px-4 py-2 bg-indigo-100 text-indigo-700 rounded-full font-medium">
                        <i class="fas fa-sitemap mr-2"></i>DFS 深度优先搜索
                    </span>
                    <span class="px-4 py-2 bg-purple-100 text-purple-700 rounded-full font-medium">
                        <i class="fas fa-layer-group mr-2"></i>BFS 广度优先搜索
                    </span>
                </div>
            </div>

            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center mb-4">
                    <i class="fas fa-clock text-3xl text-green-600 mr-4"></i>
                    <h3 class="text-2xl font-bold text-gray-800">复杂度分析</h3>
                </div>
                <div class="space-y-2">
                    <p class="text-lg">
                        <i class="fas fa-hourglass-half text-green-500 mr-2"></i>
                        <span class="font-medium">时间复杂度：</span>
                        <span class="text-green-600 font-bold">O(n)</span>
                    </p>
                    <p class="text-lg">
                        <i class="fas fa-memory text-blue-500 mr-2"></i>
                        <span class="font-medium">空间复杂度：</span>
                        <span class="text-blue-600 font-bold">O(n)</span>
                    </p>
                </div>
            </div>
        </div>

        <!-- Solution Approach -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <i class="fas fa-lightbulb text-3xl text-yellow-500 mr-4"></i>
                <h2 class="text-3xl font-bold text-gray-800">解题思路</h2>
            </div>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-indigo-50 rounded-xl p-6">
                    <h4 class="text-xl font-bold text-purple-700 mb-3">
                        <i class="fas fa-file-export mr-2"></i>序列化过程
                    </h4>
                    <p class="text-gray-700 leading-relaxed">
                        使用前序遍历，将节点值序列化为字符串，空节点用特殊字符（如 "#"）表示。这种方式能够保留树的结构信息。
                    </p>
                </div>
                <div class="bg-gradient-to-br from-indigo-50 to-purple-50 rounded-xl p-6">
                    <h4 class="text-xl font-bold text-indigo-700 mb-3">
                        <i class="fas fa-file-import mr-2"></i>反序列化过程
                    </h4>
                    <p class="text-gray-700 leading-relaxed">
                        将字符串分割为列表，使用递归或队列重建树。通过迭代器按序读取节点值，递归构建左右子树。
                    </p>
                </div>
            </div>
        </div>

        <!-- Visualization -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <div class="flex items-center mb-6">
                <i class="fas fa-project-diagram text-3xl text-teal-600 mr-4"></i>
                <h2 class="text-3xl font-bold text-gray-800">算法流程图</h2>
            </div>
            <div class="mermaid">
                graph TD
                    A[二叉树] -->|序列化| B[前序遍历]
                    B --> C[生成字符串]
                    C --> D["1,2,#,#,3,#,#"]
                    D -->|反序列化| E[分割字符串]
                    E --> F[递归重建]
                    F --> G[还原二叉树]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style G fill:#764ba2,stroke:#fff,stroke-width:3px,color:#fff
                    style D fill:#f59e0b,stroke:#fff,stroke-width:3px,color:#fff
            </div>
        </div>

        <!-- Code Implementation -->
        <div class="bg-white rounded-2xl shadow-xl overflow-hidden mb-8">
            <div class="bg-gradient-to-r from-purple-600 to-indigo-600 text-white p-6">
                <div class="flex items-center">
                    <i class="fas fa-code text-3xl mr-4"></i>
                    <h2 class="text-3xl font-bold">示例代码</h2>
                    <span class="ml-auto bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                        DFS 前序遍历实现
                    </span>
                </div>
            </div>
            <div class="code-block">
                <div class="code-header">
                    <div class="code-dot bg-red-500"></div>
                    <div class="code-dot bg-yellow-500"></div>
                    <div class="code-dot bg-green-500"></div>
                    <span class="ml-4 text-gray-400 text-sm">Python</span>
                </div>
                <pre><code><span class="keyword">class</span> <span class="function">Codec</span>:
    <span class="keyword">def</span> <span class="function">serialize</span>(self, root):
        <span class="keyword">if not</span> root:
            <span class="keyword">return</span> <span class="string">"#"</span>
        <span class="keyword">return</span> <span class="string">f"{root.val},{self.serialize(root.left)},{self.serialize(root.right)}"</span>
    
    <span class="keyword">def</span> <span class="function">deserialize</span>(self, data):
        <span class="keyword">def</span> <span class="function">dfs</span>():
            val = <span class="function">next</span>(vals)
            <span class="keyword">if</span> val == <span class="string">"#"</span>:
                <span class="keyword">return</span> <span class="keyword">None</span>
            node = <span class="function">TreeNode</span>(<span class="function">int</span>(val))
            node.left = dfs()
            node.right = dfs()
            <span class="keyword">return</span> node
        
        vals = <span class="function">iter</span>(data.split(<span class="string">","</span>))
        <span class="keyword">return</span> dfs()</code></pre>
            </div>
        </div>

        <!-- Key Points -->
        <div class="bg-gradient-to-br from-purple-600 to-indigo-600 rounded-2xl shadow-xl p-8 text-white">
            <div class="flex items-center mb-6">
                <i class="fas fa-key text-4xl mr-4"></i>
                <h2 class="text-3xl font-bold">关键要点</h2>
            </div>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white bg-opacity-10 rounded-xl p-6 backdrop-filter backdrop-blur-sm">
                    <i class="fas fa-check-circle text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">选择合适的遍历方式</h4>
                    <p class="opacity-90">前序遍历能够方便地进行递归重建</p>
                </div>
                <div class="bg-white bg-opacity-10 rounded-xl p-6 backdrop-filter backdrop-blur-sm">
                    <i class="fas fa-hashtag text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">空节点的标记</h4>
                    <p class="opacity-90">使用特殊字符标记空节点，保留树结构</p>
                </div>
                <div class="bg-white bg-opacity-10 rounded-xl p-6 backdrop-filter backdrop-blur-sm">
                    <i class="fas fa-sync-alt text-3xl mb-3"></i>
                    <h4 class="text-xl font-bold mb-2">迭代器的使用</h4>
                    <p class="opacity-90">利用迭代器按序读取，简化代码逻辑</p>
                </div>
            </div>
        </div>
    </main>

    <script>
        mermaid.initialize({ 
            startOnLoad: true,
            theme: 'default',
            themeVariables: {
                primaryColor: '#667eea',
                primaryTextColor: '#fff',
                primaryBorderColor: '#7c3aed',
                lineColor: '#5b21b6',
                secondaryColor: '#764ba